import { Navigate, useLocation } from 'react-router-dom';
import { ReactNode, FC } from 'react';

interface PrivateRouteProps {
  children: ReactNode; // ReactNode 可以接受任何 React 子元素
}

const PrivateRoute: FC<PrivateRouteProps> = ({ children }) => {
  const token = sessionStorage.getItem('token');
  const location = useLocation();

  if (token) {
    return <>{children}</>;
  } else {
    // 跳转首页，添加redirect查询参数
    return <Navigate to={`/?redirect=${encodeURIComponent(location.pathname)}`} replace />;
  }
};

export default PrivateRoute;